package com.firebird.app.kotlin.stock.base

import android.content.Context
import android.content.Intent
import android.content.pm.ActivityInfo
import android.os.Bundle
import androidx.annotation.Nullable
import androidx.appcompat.app.AppCompatActivity
import butterknife.ButterKnife
import butterknife.Unbinder
import com.firebird.app.kotlin.stock.myview.state_layout.OtherViewHolder
import com.gyf.barlibrary.ImmersionBar
import com.firebird.app.kotlin.stock.R
import com.firebird.app.kotlin.stock.utils.SharedPreferencesUtil
import java.util.*


abstract class BaseActivity : AppCompatActivity() {
    private var unbinder: Unbinder? = null
    var savedInstanceState: Bundle? = null
    var mHolder: OtherViewHolder? = null
    var isNetWork = true
    var strUserid: String? = null
    var keyList: MutableList<String> = ArrayList()
    var valueList: MutableList<String> = ArrayList()
    override fun onCreate(@Nullable savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //禁止横屏
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        setContentView(layoutId)
        this.savedInstanceState = savedInstanceState
        unbinder = ButterKnife.bind(this)
        ImmersionBar.with(this).transparentStatusBar().statusBarDarkFont(true).init()
        strUserid = SharedPreferencesUtil.getInstance(this).getSP("debtorid")
        //空白页初始化
        mHolder = OtherViewHolder(this)
        initView()
    }

    override fun onDestroy() {
        super.onDestroy()
        unbinder!!.unbind()
        ImmersionBar.with(this).destroy()
    }

    /**
     * 设置布局
     *
     * @return
     */
    abstract val layoutId: Int

    /**
     * 初始化视图
     */
    abstract fun initView()

    protected fun toClass(context: Context, clazz: Class<out BaseMvpActivity?>?) {
        val intent = Intent(context, clazz)
        context.startActivity(intent)
    }

    protected fun toClass1(context: Context, clazz: Class<out BaseActivity?>?) {
        val intent = Intent(context, clazz)
        context.startActivity(intent)
    }

    protected fun toClass1(context: Context, clazz: Class<out BaseActivity?>?, bundle: Bundle?) {
        val intent = Intent(context, clazz)
        intent.putExtras(bundle)
        context.startActivity(intent)
    }

    //带参数
    protected fun toClass(context: Context, clazz: Class<out BaseMvpActivity?>?, bundle: Bundle?) {
        val intent = Intent(context, clazz)
        intent.putExtras(bundle)
        context.startActivity(intent)
    }

    protected fun toClass_Empty(context: Context, clazz: Class<out BaseActivity?>?) {
        val intent = Intent(context, clazz)
        intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
        context.startActivity(intent)
    }

    protected fun toClass_EmptyTop(context: Context, clazz: Class<out BaseActivity?>?) {
        val intent = Intent(context, clazz)
        intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
        //如果不想创建新的Activity则加上这句
//        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent)
        finish()
    }

    protected fun toClass(context: Context?, clazz: Class<out BaseMvpActivity?>?, bundle: Bundle?, reuqestCode: Int) {
        val intent = Intent(context, clazz)
        intent.putExtras(bundle)
        startActivityForResult(intent, reuqestCode)
    }

    protected fun toClass1(context: Context?, clazz: Class<out BaseActivity?>?, bundle: Bundle?, reuqestCode: Int) {
        val intent = Intent(context, clazz)
        intent.putExtras(bundle)
        startActivityForResult(intent, reuqestCode)
    }

    protected fun toClass(context: Context?, clazz: Class<out BaseMvpActivity?>?, reuqestCode: Int) {
        val intent = Intent(context, clazz)
        startActivityForResult(intent, reuqestCode)
    }

    protected fun toClass1(context: Context?, clazz: Class<out BaseActivity?>?, reuqestCode: Int) {
        val intent = Intent(context, clazz)
        startActivityForResult(intent, reuqestCode)
    }
}